/*
* @Author: majingyu
* @Date:   2017-05-04 10:43:41
* @Last Modified by:   jingyu.ma
* @Last Modified time: 2017-08-07 22:34:57
*/
import Fetch from 'common/fetch';
import { getParams } from 'common/getParams';
//查找不同分类周边服务信息
const eidtPeripheralService = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'EIDT_SERVICE_INFO_SUCCESS'){
        action['data'] = data;
    }else if(type == 'EIDT_SERVICE_INFO_ERROR'){
        action['e'] = data;
    }
    return action;
}

const EIDT_SERVICE_INFO_BEGIN = 'EIDT_SERVICE_INFO_BEGIN';
const EIDT_SERVICE_INFO_SUCCESS = 'EIDT_SERVICE_INFO_SUCCESS';
const EIDT_SERVICE_INFO_ERROR = 'EIDT_SERVICE_INFO_ERROR';

export const eidtServiceInfoAction = (params) => (dispatch, getState) => {
    dispatch(eidtPeripheralService(EIDT_SERVICE_INFO_BEGIN)); 
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/demand/nearby?'+paramsList,{
        credentials:'include'
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(eidtPeripheralService(EIDT_SERVICE_INFO_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(eidtPeripheralService(EIDT_SERVICE_INFO_ERROR,data));
        }
    }).catch(e=>{
        dispatch(eidtPeripheralService(EIDT_SERVICE_INFO_ERROR,e));
    })
}

//查找需求列表
const editDemandListType = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'EIDT_DEMAND_LIST_SUCCESS'){
        action['data'] = data;
    }else if(type == 'EIDT_DEMAND_LIST_ERROR'){
        action['e'] = data;
    }
    return action;
}

const EIDT_DEMAND_LIST_BEGIN = 'EIDT_DEMAND_LIST_BEGIN';
const EIDT_DEMAND_LIST_SUCCESS = 'EIDT_DEMAND_LIST_SUCCESS';
const EIDT_DEMAND_LIST_ERROR = 'EIDT_DEMAND_LIST_ERROR';

export const editDemandList = (params) => (dispatch, getState) => {
    dispatch(editDemandListType(EIDT_DEMAND_LIST_BEGIN)); 
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/demand/search?'+paramsList,{
        credentials:'include'
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(editDemandListType(EIDT_DEMAND_LIST_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(editDemandListType(EIDT_DEMAND_LIST_ERROR,data));
        }
    }).catch(e=>{
        dispatch(editDemandListType(EIDT_DEMAND_LIST_ERROR,e));
    })
}

//查找周边所有的服务
const searchAllSevices = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'SEARCH_ALL_SEVICES_SUCCESS'){
        action['data'] = data;
    }else if(type == 'SEARCH_ALL_SEVICES_ERROR'){
        action['e'] = data;
    }
    return action;
}

const SEARCH_ALL_SEVICES_BIGIN = 'SEARCH_ALL_SEVICES_BIGIN';
const SEARCH_ALL_SEVICES_SUCCESS = 'SEARCH_ALL_SEVICES_SUCCESS';
const SEARCH_ALL_SEVICES_ERROR = 'SEARCH_ALL_SEVICES_ERROR';

export const searchAllServiceInfoAction = (params) => (dispatch, getState) => {
    dispatch(searchAllSevices(SEARCH_ALL_SEVICES_BIGIN));
    let paramlist = params?getParams(params):'';
    let url = paramlist?'/wx/course/home?'+paramlist:'/wx/course/home';

    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch(url).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(searchAllSevices(SEARCH_ALL_SEVICES_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(searchAllSevices(SEARCH_ALL_SEVICES_ERROR,data));
        }
    }).catch(e=>{
        dispatch(searchAllSevices(SEARCH_ALL_SEVICES_ERROR,e));
    })
}
const eidtServiceTabAction = (type, data) => {
    let action = {
        type: type,
        data: data
    };
    return action;
}
const TAB_IS_SERVICESLIST = 'TAB_IS_SERVICESLIST';
const TAB_IS_SKILLLIST = 'TAB_IS_SKILLLIST';
//判断是服务列表还是需求列表
export const eidtServiceTab = (tabName) => (dispatch, getState) =>{
    if(tabName == 1){
        dispatch(eidtServiceTabAction(TAB_IS_SERVICESLIST, tabName)); 
    }else if(tabName == 2){
        dispatch(eidtServiceTabAction(TAB_IS_SKILLLIST, tabName)); 
    }
    
}

//修改选中信息
const EDIT_CALSS_CHECK = 'EDIT_CALSS_CHECK';

export const editCalssCheck = (message)=>{
    return {
        type: EDIT_CALSS_CHECK,
        message
    }
}
const EDIT_COURSE_DEMAND_LIST = 'EDIT_COURSE_DEMAND_LIST';
export const editDemandId = (data)=>{
    return {
        type: EDIT_COURSE_DEMAND_LIST,
        data
    }
}
import { toast } from 'common/tips.js';


//查找排序信息
const editSort = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'EDIT_SORT_SERVICE_SUCCESS'){
        action['data'] = data;
    }else if(type == 'EDIT_SORT_SERVICE_ERROR'){
        action['e'] = data;
    }
    return action;
}

const EDIT_SORT_SERVICE_BIGIN = 'EDIT_SORT_SERVICE_BIGIN';
const EDIT_SORT_SERVICE_SUCCESS = 'EDIT_SORT_SERVICE_SUCCESS';
const EDIT_SORT_SERVICE_ERROR = 'EDIT_SORT_SERVICE_ERROR';

export const editSortList = () => (dispatch, getState) => {
    dispatch(editSort(EDIT_SORT_SERVICE_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/sevices/sortList',{
        method:'POST',
        // body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(editSort(EDIT_SORT_SERVICE_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(editSort(EDIT_SORT_SERVICE_ERROR,data));
        }
    }).catch(e=>{
        dispatch(editSort(EDIT_SORT_SERVICE_ERROR,e));
    })
}
//查找筛选信息
const editFilter = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'EDIT_FILTER_SERVICE_SUCCESS'){
        action['data'] = data;
    }else if(type == 'EDIT_FILTER_SERVICE_ERROR'){
        action['e'] = data;
    }
    return action;
}

const EDIT_FILTER_SERVICE_BIGIN = 'EDIT_FILTER_SERVICE_BIGIN';
const EDIT_FILTER_SERVICE_SUCCESS = 'EDIT_FILTER_SERVICE_SUCCESS';
const EDIT_FILTER_SERVICE_ERROR = 'EDIT_FILTER_SERVICE_ERROR';

export const editFilterList = () => (dispatch, getState) => {
    dispatch(editFilter(EDIT_FILTER_SERVICE_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/sevices/varfilterList',{
        method:'POST',
        // body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(editFilter(EDIT_FILTER_SERVICE_SUCCESS,data.data));
        }else{
            message.error(data.err)
            dispatch(editFilter(EDIT_FILTER_SERVICE_ERROR,data));
        }
    }).catch(e=>{
        dispatch(editFilter(EDIT_FILTER_SERVICE_ERROR,e));
    })
}

//查看详情界面  START
//1、修改选中查看详情的ID
//修改信息ID
const CHANGE_DETAIL_ID = 'CHANGE_DETAIL_ID';
export const changeDetailId = (id) => {
    return {
        type:CHANGE_DETAIL_ID,
        id
    }
}

//2、查看详情(技能)
//获取摸个某个的详情
const getDetailype = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_DETAIL_SUCCESS){
        action['data'] = data;
    }else if(type == GET_DETAIL_ERROR){
        action['e'] = data;
    }else if(type == GET_DETAIL_BIGIN){
        action['data'] = data;
    }
    return action;
}
const GET_DETAIL_BIGIN = 'GET_DETAIL_BIGIN';
const GET_DETAIL_SUCCESS = 'GET_DETAIL_SUCCESS';
const GET_DETAIL_ERROR = 'GET_DETAIL_ERROR';

export const getSkillDetails = (params) => (dispatch, getState) => {
    dispatch(getDetailype(GET_DETAIL_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/skill/detail',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getDetailype(GET_DETAIL_SUCCESS, data.data));
        }else{
            message.error(data.err)
            dispatch(getDetailype(GET_DETAIL_ERROR,data.error));
        }
    }).catch(e=>{
        dispatch(getDetailype(GET_DETAIL_ERROR,e));
    })
}

//2、查看详情(需求)
//获取摸个某个的详情
const getServiceDetailype = (type, data) => {
    let action = {
        type:type
    }
    if(type == GET_SERVICES_DETAIL_SUCCESS){
        action['data'] = data;
    }else if(type == GET_SERVICES_DETAIL_ERROR){
        action['e'] = data;
    }else if(type == GET_SERVICES_DETAIL_BIGIN){
        action['data'] = data;
    }
    return action;
}
const GET_SERVICES_DETAIL_BIGIN = 'GET_SERVICES_DETAIL_BIGIN';
const GET_SERVICES_DETAIL_SUCCESS = 'GET_SERVICES_DETAIL_SUCCESS';
const GET_SERVICES_DETAIL_ERROR = 'GET_SERVICES_DETAIL_ERROR';

export const getServiceDetails = (params) => (dispatch, getState) => {
    dispatch(getDetailype(GET_SERVICES_DETAIL_BIGIN));
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/demand/detail',{
        method:'POST',
         body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getDetailype(GET_SERVICES_DETAIL_SUCCESS, data.data));
        }else{
            message.error(data.err)
            dispatch(getDetailype(GET_SERVICES_DETAIL_ERROR,data.error));
        }
    }).catch(e=>{
        dispatch(getDetailype(GET_SERVICES_DETAIL_ERROR,e));
    })
}



//修改serviceType,判断是查找需求列表还是技能列表
const CHANGE_PAGE_TYPE = 'CHANGE_PAGE_TYPE';
export const changePageType = (status) => {
    let type = status == 1?'servicesList':'skillList';
    return {
        type:CHANGE_PAGE_TYPE,
        type
    }
}
//修改historyPage,判断显示的按钮是约吧还是接受
const CHANGE_HISTORY_PAGE_TYPE = 'CHANGE_HISTORY_PAGE_TYPE';
export const changeHistoryPageType = (type) => {
    return {
        type:CHANGE_HISTORY_PAGE_TYPE,
        type
    }
}

//点赞
export const addTips = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/web/service/zan',{
        method:'POST',
        body:JSON.stringify(params),
        credentials:'include',
        headers:myHeaders
    }).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            toast("点赞成功")
           dispatch(eidtServiceInfoAction());
        }else{
            toast(data.err)
        }
    }).catch(e=>{
        toast('点赞失败')
    })
}

//接受
export const accectOrder = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/order/accept?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            toast("已接受")
           dispatch(eidtServiceInfoAction());
        }else{
            toast(data.err)
        }
    }).catch(e=>{
        toast('操作失败')
    })
}

//拒绝
export const rejectOrder = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/order/cancel?'+paramsList).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            toast("已拒绝")
           dispatch(eidtServiceInfoAction());
        }else{
            toast(data.err)
        }
    }).catch(e=>{
        toast('操作失败')
    })
}

//约单(需求)
export const invitesDemandOrder = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
     let paramsList = getParams(params);
    return Fetch('/wx/demand/invite?'+paramsList,).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            toast("发布请求给对方")
        }else{
            toast(data.err)
        }
    }).catch(e=>{
        toast('操作失败')
    })
}
//约单（技能）
export const invitesSkillOrder = (params) => (dispatch, getState) => {
    let myHeaders = new Headers();
    myHeaders.append('Content-Type', 'application/json');
     let paramsList = getParams(params);
    return Fetch('/wx/skill/invite?'+paramsList,).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            toast("发布请求给对方")
        }else{
            toast(data.err)
        }
    }).catch(e=>{
        toast('操作失败')
    })
}

//查找不同分类周边技能信息
const eidtPeripheralSkill = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'EIDT_SKILL_INFO_SUCCESS'){
        action['data'] = data;
    }else if(type == 'EIDT_SKILL_INFO_ERROR'){
        action['e'] = data;
    }
    return action;
}

const EIDT_SKILL_INFO_BEGIN = 'EIDT_SKILL_INFO_BEGIN';
const EIDT_SKILL_INFO_SUCCESS = 'EIDT_SKILL_INFO_SUCCESS';
const EIDT_SKILL_INFO_ERROR = 'EIDT_SKILL_INFO_ERROR';

export const eidtSkillInfoAction = (params) => (dispatch, getState) => {
    dispatch(eidtPeripheralSkill(EIDT_SKILL_INFO_BEGIN))
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/skill/nearby?'+paramsList,{credentials:'include'}).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(eidtPeripheralSkill(EIDT_SKILL_INFO_SUCCESS,data.data));
        }else{
            console.log("error");
            message.error(data.err)
            dispatch(eidtPeripheralSkill(EIDT_SKILL_INFO_ERROR,data));
        }
    }).catch(e=>{
        console.log("catch error")
        dispatch(eidtPeripheralSkill(EIDT_SKILL_INFO_ERROR,e));
    })
}

//查找不同分类周边技能信息
const getMyInviteType = (type, data) => {
    let action = {
        type:type
    }
    if(type == 'GET_MY_INVITE_SUCCESS'){
        action['data'] = data;
    }else if(type == 'GET_MY_INVITE_ERROR'){
        action['e'] = data;
    }
    return action;
}

const GET_MY_INVITE_BEGIN = 'GET_MY_INVITE_BEGIN';
const GET_MY_INVITE_SUCCESS = 'GET_MY_INVITE_SUCCESS';
const GET_MY_INVITE_ERROR = 'GET_MY_INVITE_ERROR';

export const getMyList = (params) => (dispatch, getState) => {
    console.log("hhhrrr")
    dispatch(getMyInviteType(GET_MY_INVITE_BEGIN))
    let myHeaders = new Headers();
    let paramsList = getParams(params);
    myHeaders.append('Content-Type', 'application/json');
    return Fetch('/wx/order/orderPage?'+paramsList,{credentials:'include'}).then(res=>res.json()).then((data)=>{
        if(data.status == 200){
            dispatch(getMyInviteType(GET_MY_INVITE_SUCCESS,data.data));
        }else{
            console.log("error");
            message.error(data.err)
            dispatch(getMyInviteType(GET_MY_INVITE_ERROR,data));
        }
    }).catch(e=>{
        dispatch(getMyInviteType(GET_MY_INVITE_ERROR,e));
    })
}
